//
//  PicturePickerCell.swift
//  SelectPhotos
//
//  Created by Wai Cheung Chiu on 2017/6/23.
//  Copyright © 2017年 李锡鹏. All rights reserved.
//

import UIKit

@objc protocol PicturePickerCellDelegate:NSObjectProtocol
{
    // 添加照片
    @objc optional func PicturePickerCellAdd(cell:PicturePickerCell)
    // 删除照片
    @objc optional func PicturePickerCellDelete(cell:PicturePickerCell)
}

class PicturePickerCell:UICollectionViewCell{
    
    weak var picturePickerDelegate : PicturePickerCellDelegate?
    var image : UIImage? {
        didSet {
            addButton.setImage(image ?? UIImage(named:"compose_pic_add.png") , for: .normal)
            deleteButton.isHidden = (image == nil)
        }
    }
    func addPicture(){
        print("加图")
        picturePickerDelegate?.PicturePickerCellAdd!(cell: self)
    }
    func deletePicture(){
        print("删图")
        picturePickerDelegate?.PicturePickerCellDelete!(cell: self)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI() {
        self.addSubview(addButton)
        self.addSubview(deleteButton)
        addButton.frame = bounds
        
        deleteButton.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(25)
            make.width.equalTo(25)
        }
        
    }
    lazy var addButton:UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named:"compose_pic_add.png"), for: .normal)
        //button.setImage(UIImage(named:"compose_pic_add_highlighted.png"), for: .highlighted)
        button.sizeToFit()
        button.imageView?.contentMode = .scaleAspectFill
        button.addTarget(self, action: #selector(addPicture), for: UIControlEvents.touchUpInside)
        return button
    }()
    lazy var deleteButton:UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named:"compose_delete.png"), for: .normal)
        button.sizeToFit()
        button.addTarget(self, action: #selector(deletePicture), for: UIControlEvents.touchUpInside)
        return button
    }()
}
